﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes.orders {
    import flash.utils.*;

    public class OrderManager {

        private var m_orders:Dictionary;

        public function OrderManager(){
            this.m_orders = new Dictionary();
        }
        private function updateOrderParams(_arg1:String, _arg2:String, _arg3:String, _arg4:String, _arg5:Object):Boolean{
            var _local6:Object;
            var _local7:AbstractOrder;
            if (this.m_orders[_arg1]){
                if (this.m_orders[_arg1][_arg2]){
                    for each (_local6 in this.m_orders[_arg1][_arg2]) {
                        if ((_local6 is AbstractOrder)){
                            _local7 = (_local6 as AbstractOrder);
                            if (_local7.equals(new AbstractOrder(_arg3, _arg4, _arg1, _arg2))){
                                _local7.updateOrder(_arg5);
                                return (true);
                            };
                        };
                    };
                };
            };
            return (false);
        }
        public function addOrdersByType(_arg1:String, _arg2:Object, _arg3:Boolean=false):int{
            var _local5:String;
            var _local6:String;
            var _local7:String;
            var _local8:String;
            var _local9:String;
            var _local10:AbstractOrder;
            var _local4:int;
            if (!this.m_orders[_arg1]){
                this.m_orders[_arg1] = new Dictionary();
            };
            for (_local5 in _arg2[_arg1]) {
                if (!this.m_orders[_arg1][_local5]){
                    this.m_orders[_arg1][_local5] = new Array();
                };
                for (_local6 in _arg2[_arg1][_local5]) {
                    for (_local7 in _arg2[_arg1][_local5][_local6]) {
                        _local8 = ((_local5)==OrderStatus.RECEIVED) ? Global.player.snUser.uid : _local7;
                        _local9 = ((_local5)==OrderStatus.RECEIVED) ? _local7 : Global.player.snUser.uid;
                        if (((_arg3) || (!(this.updateOrderParams(_arg1, _local5, _local8, _local9, _arg2[_arg1][_local5][_local6][_local7]))))){
                            _local10 = OrderType.order(_local8, _local9, _arg1, _local5, _arg2[_arg1][_local5][_local6][_local7]);
                            if (_local10 == null){
                            } else {
                                _local10.setState(_local6);
                                _local10.setTransmissionStatus(_local5);
                                this.m_orders[_arg1][_local5].push(_local10);
                                _local4++;
                            };
                        };
                    };
                };
            };
            return (_local4);
        }
        public function addOrders(_arg1:Object, _arg2:Boolean=false):void{
            var _local3:String;
            for (_local3 in _arg1) {
                this.addOrdersByType(_local3, _arg1, _arg2);
            };
        }
        public function initializeOrders(_arg1:Object):void{
            this.m_orders = new Dictionary();
            this.addOrders(_arg1, true);
        }
        public function getOrders(_arg1:String, _arg2:String, _arg3:String=null):Array{
            var _local5:Array;
            var _local6:Array;
            var _local7:AbstractOrder;
            var _local8:String;
            var _local4:Dictionary = this.m_orders[_arg1];
            if (_local4){
                _local5 = _local4[_arg2];
                if (_local5){
                    if (!_arg3){
                        return (_local5);
                    };
                    _local6 = new Array();
                    for each (_local7 in _local5) {
                        _local8 = _local7.getState();
                        if (_local7.getState() == _arg3){
                            _local6.push(_local7);
                        };
                    };
                    return (_local6);
                };
            };
            return ([]);
        }
        public function getOrdersByType(_arg1:String):Array{
            var _local4:Array;
            var _local2:Array = new Array();
            var _local3:Dictionary = this.m_orders[_arg1];
            if (_local3){
                for each (_local4 in _local3) {
                    _local2 = _local2.concat(_local4);
                };
            };
            return (_local2);
        }
        public function getOrdersByStatus(_arg1:String):Array{
            var _local3:Dictionary;
            var _local2:Array = new Array();
            for each (_local3 in this.m_orders) {
                if ((_local3[_arg1] is Array)){
                    _local2 = _local2.concat(_local3[_arg1]);
                };
            };
            return (_local2);
        }
        public function placeOrder(_arg1:AbstractOrder):void{
            var _local2:String = _arg1.getType();
            var _local3:String = _arg1.getTransmissionStatus();
            if (this.m_orders[_local2] == null){
                this.m_orders[_local2] = new Dictionary();
            };
            if (this.m_orders[_local2][_local3] == null){
                this.m_orders[_local2][_local3] = new Array();
            };
            _arg1.setTimeSent(uint((GlobalEngine.getTimer() / 1000)));
            this.m_orders[_local2][_local3].push(_arg1);
        }
        public function getOrdersByUID(_arg1:String, _arg2:String="", _arg3:String=""):Array{
            var _local5:Dictionary;
            var _local6:AbstractOrder;
            var _local7:String;
            var _local8:Boolean;
            var _local9:Boolean;
            var _local4:Array = new Array();
            for each (_local5 in this.m_orders) {
                if ((_local5[_arg3] is Array)){
                    for each (_local6 in _local5[_arg3]) {
                        _local7 = ((_arg3)==OrderStatus.RECEIVED) ? _local6.getSenderID() : _local6.getRecipientID();
                        _local8 = Boolean((_local7 == _arg1));
                        _local9 = (_arg2) ? Boolean((_arg2 == _local6.getType())) : true;
                        if (((_local8) && (_local9))){
                            _local4 = _local4.concat(_local6);
                        };
                    };
                };
            };
            return (_local4);
        }
        public function updateOrder(_arg1:AbstractOrder):void{
            var _local2:String = _arg1.getType();
            var _local3:String = _arg1.getTransmissionStatus();
            if ((((this.m_orders[_local2] == null)) || (this.m_orders[_local2][_local3]))){
                return;
            };
            var _local4:int;
            while (_local4 < this.m_orders[_local2][_local3].length()) {
                if (this.m_orders[_local2][_local3][_local4].getSenderID() == _arg1.getSenderID()){
                    this.m_orders[_local2][_local3][_local4] = _arg1;
                    break;
                };
                _local4++;
            };
        }
        public function removeOrder(_arg1:AbstractOrder):void{
            var _local2:String = _arg1.getType();
            var _local3:String = _arg1.getTransmissionStatus();
            if ((((this.m_orders[_local2] == null)) || ((this.m_orders[_local2][_local3] == null)))){
                return;
            };
            var _local4:int;
            while (_local4 < this.m_orders[_local2][_local3].length) {
                if (this.m_orders[_local2][_local3][_local4].equals(_arg1)){
                    this.m_orders[_local2][_local3].splice(_local4, 1);
                    break;
                };
                _local4++;
            };
        }
        public function cleanUp():void{
            this.m_orders = null;
        }

    }
}//package Classes.orders 
